<html lang="zh-CN"><head><meta charset="UTF-8"><style>.nodata  main {width:1000px;margin: auto;}</style></head><body class="nodata " style=""><div class="main_father clearfix d-flex justify-content-center " style="height:100%;"> <div class="container clearfix " id="mainBox"><main><div class="blog-content-box">
<div class="article-header-box">
<div class="article-header">
<div class="article-title-box">
<h1 class="title-article" id="articleContentId">(B卷,100分)- VLAN资源池（Java & JS & Python）</h1>
</div>
</div>
</div>
<div id="blogHuaweiyunAdvert"></div>

        <div id="article_content" class="article_content clearfix">
        <link rel="stylesheet" href="https://csdnimg.cn/release/blogv2/dist/mdeditor/css/editerView/kdoc_html_views-1a98987dfd.css">
        <link rel="stylesheet" href="https://csdnimg.cn/release/blogv2/dist/mdeditor/css/editerView/ck_htmledit_views-044f2cf1dc.css">
                <div id="content_views" class="htmledit_views">
                    <h4 id="main-toc">题目描述</h4> 
<p>VLAN是一种对局域网设备进行逻辑划分的技术&#xff0c;为了标识不同的VLAN&#xff0c;引入VLAN ID(1-4094之间的整数)的概念。</p> 
<p>定义一个VLAN ID的资源池(下称VLAN资源池)&#xff0c;资源池中连续的VLAN用开始VLAN-结束VLAN表示&#xff0c;不连续的用单个整数表示&#xff0c;所有的VLAN用英文逗号连接起来。</p> 
<p>现在有一个VLAN资源池&#xff0c;业务需要从资源池中申请一个VLAN&#xff0c;需要你输出从VLAN资源池中移除申请的VLAN后的资源池。</p> 
<p></p> 
<h4 id="%E8%BE%93%E5%85%A5%E6%8F%8F%E8%BF%B0">输入描述</h4> 
<p>第一行为字符串格式的VLAN资源池&#xff0c;第二行为业务要申请的VLAN&#xff0c;VLAN的取值范围为[1,4094]之间的整数。</p> 
<p></p> 
<h4 id="%E8%BE%93%E5%87%BA%E6%8F%8F%E8%BF%B0">输出描述</h4> 
<p>从输入VLAN资源池中移除申请的VLAN后字符串格式的VLAN资源池&#xff0c;输出要求满足题目描述中的格式&#xff0c;并且按照VLAN从小到大升序输出。</p> 
<p>如果申请的VLAN不在原VLAN资源池内&#xff0c;输出原VLAN资源池升序排序后的字符串即可。</p> 
<p></p> 
<h4 id="%E5%A4%87%E6%B3%A8%C2%A0">备注 </h4> 
<p>输入VLAN资源池中VLAN的数量取值范围为[2-4094]间的整数&#xff0c;资源池中VLAN不重复且合法([1,4094]之间的整数)&#xff0c;输入是乱序的。</p> 
<p></p> 
<h4 id="%E7%94%A8%E4%BE%8B">用例</h4> 
<table border="1" cellpadding="1" cellspacing="1" style="width:500px;"><tbody><tr><td style="width:86px;">输入</td><td style="width:412px;"> <p>1-5<br /> 2</p> </td></tr><tr><td style="width:86px;">输出</td><td style="width:412px;">1,3-5</td></tr><tr><td style="width:86px;">说明</td><td style="width:412px;">原VLAN资源池中有VLAN 1、2、3、4、5&#xff0c;从资源池中移除2后&#xff0c;剩下VLAN 1、3、4、5&#xff0c;按照题目描述格式并升序后的结果为1,3-5。</td></tr></tbody></table> 
<table border="1" cellpadding="1" cellspacing="1" style="width:500px;"><tbody><tr><td style="width:86px;">输入</td><td style="width:412px;"> <p>20-21,15,18,30,5-10<br /> 15</p> </td></tr><tr><td style="width:86px;">输出</td><td style="width:412px;">5-10,18,20-21,30</td></tr><tr><td style="width:86px;">说明</td><td style="width:412px;">原VLAN资源池中有VLAN 5、6、7、8、9、10、15、18、20、21、30&#xff0c;从资源池中移除15后&#xff0c;资源池中剩下的VLAN为 5、6、7、8、9、10、18、20、21、30&#xff0c;按照题目描述格式并升序后的结果为5-10,18,20-21,30。</td></tr></tbody></table> 
<table border="1" cellpadding="1" cellspacing="1" style="width:500px;"><tbody><tr><td style="width:86px;">输入</td><td style="width:412px;"> <p>5,1-3<br /> 10</p> </td></tr><tr><td style="width:86px;">输出</td><td style="width:412px;">1-3,5</td></tr><tr><td style="width:86px;">说明</td><td style="width:412px;">原VLAN资源池中有VLAN 1、2、3&#xff0c;5&#xff0c;申请的VLAN 10不在原资源池中&#xff0c;将原资源池按照题目描述格式并按升序排序后输出的结果为1-3,5。</td></tr></tbody></table> 
<p></p> 
<h4 id="%E9%A2%98%E7%9B%AE%E8%A7%A3%E6%9E%90">题目解析</h4> 
<p>本题主要考察逻辑分析能力&#xff0c;以及一些边际情况的处理。比如1-2删除1&#xff0c;或者1-3删除3等。</p> 
<p></p> 
<p>我的解题思路如下&#xff1a;</p> 
<p>将输入的第一行转化为一个二维数组&#xff0c;比如用例2第一行输入可以转化为&#xff1a;</p> 
<p>[ [20,21], [15], [18], [30], [5,10] ]</p> 
<p>之后&#xff0c;将这个二维数组&#xff0c;按照元素的第0个索引的值进行升序排序&#xff0c;得到如下</p> 
<p>[ [5,10], [15], [18], [20,21], [30] ]</p> 
<p>然后从左到右遍历各个区间&#xff0c;看要删除的数remove&#xff08;第二行输入的数&#xff09;是否在区间[from, to]内&#xff0c;如果在则删除&#xff0c;删除时需要注意&#xff1a;</p> 
<ul><li>如果要删除的数&#xff0c;是区间左边界&#xff0c;则区间变为[remove&#43;1, to]</li><li>如果要删除的数&#xff0c;是区间右边界&#xff0c;则区间变为[from, remove-1]</li><li>如果要删除的数&#xff0c;在区间内&#xff0c;则区间变为[from, remove-1]&#xff0c;和[remove&#43;1, to]</li></ul> 
<p>需要注意的是&#xff0c;如果区间的左右边界相同&#xff0c;比如[3,3]&#xff0c;此时应该将区间变为[3]</p> 
<p></p> 
<h4>Java算法源码</h4> 
<pre><code class="language-java">import java.util.Arrays;
import java.util.LinkedList;
import java.util.Scanner;
import java.util.StringJoiner;
import java.util.stream.Collectors;

public class Main {
  public static void main(String[] args) {
    Scanner sc &#61; new Scanner(System.in);

    String[] vlanArr &#61; sc.nextLine().split(&#34;,&#34;);
    int remove &#61; Integer.parseInt(sc.nextLine());

    System.out.println(getResult(vlanArr, remove));
  }

  public static String getResult(String[] vlanArr, int remove) {
    LinkedList&lt;Integer[]&gt; vlanList &#61;
        Arrays.stream(vlanArr)
            .map(v -&gt; Arrays.stream(v.split(&#34;-&#34;)).map(Integer::parseInt).toArray(Integer[]::new))
            .sorted((a, b) -&gt; a[0] - b[0])
            .collect(Collectors.toCollection(LinkedList::new));

    for (int i &#61; 0; i &lt; vlanList.size(); i&#43;&#43;) {
      Integer[] vlan &#61; vlanList.get(i);

      int from &#61; vlan[0];

      if (vlan.length &gt; 1) {
        int to &#61; vlan[1];

        if (remove &lt; from || remove &gt; to) continue;

        vlanList.remove(i);

        if (remove &#61;&#61; from) {
          vlanList.add(i, generateRange(remove &#43; 1, to));
        } else if (remove &#61;&#61; to) {
          vlanList.add(i, generateRange(from, remove - 1));
        } else {
          vlanList.add(i, generateRange(remove &#43; 1, to));
          vlanList.add(i, generateRange(from, remove - 1));
        }

        break;
      } else if (from &#61;&#61; remove) {
        vlanList.remove(i);
        break;
      }
    }

    StringJoiner ans &#61; new StringJoiner(&#34;,&#34;);

    vlanList.stream()
        .map(
            vlan -&gt; {
              StringJoiner sj &#61; new StringJoiner(&#34;-&#34;);
              for (Integer v : vlan) sj.add(v &#43; &#34;&#34;);
              return sj.toString();
            })
        .forEach(ans::add);

    return ans.toString();
  }

  public static Integer[] generateRange(int from, int to) {
    if (from &lt; to) {
      return new Integer[] {from, to};
    } else {
      return new Integer[] {from};
    }
  }
}
</code></pre> 
<p></p> 
<h4 id="%E7%AE%97%E6%B3%95%E6%BA%90%E7%A0%81">JS算法源码</h4> 
<pre><code class="language-javascript">/* JavaScript Node ACM模式 控制台输入获取 */
const readline &#61; require(&#34;readline&#34;);

const rl &#61; readline.createInterface({
  input: process.stdin,
  output: process.stdout,
});

const lines &#61; [];
rl.on(&#34;line&#34;, (line) &#61;&gt; {
  lines.push(line);

  if (lines.length &#61;&#61;&#61; 2) {
    let vlanArr &#61; lines[0].split(&#34;,&#34;);
    let remove &#61; parseInt(lines[1]);

    console.log(getResult(vlanArr, remove));

    lines.length &#61; 0;
  }
});

function getResult(vlanArr, remove) {
  vlanArr &#61; vlanArr
    .map((vlan) &#61;&gt; vlan.split(&#34;-&#34;).map(Number))
    .sort((a, b) &#61;&gt; a[0] - b[0]);

  for (let i &#61; 0; i &lt; vlanArr.length; i&#43;&#43;) {
    let [from, to] &#61; vlanArr[i];

    if (to !&#61; undefined) {
      if (remove &lt; from || remove &gt; to) continue;

      if (remove &#61;&#61; from) {
        vlanArr.splice(i, 1, generateRange(remove &#43; 1, to));
      } else if (remove &#61;&#61; to) {
        vlanArr.splice(i, 1, generateRange(from, remove - 1));
      } else {
        vlanArr.splice(
          i,
          1,
          generateRange(from, remove - 1),
          generateRange(remove &#43; 1, to)
        );
      }
      break;
    } else if (from &#61;&#61;&#61; remove) {
      vlanArr.splice(i, 1);
      break;
    }
  }

  return vlanArr.map((vlan) &#61;&gt; vlan.join(&#34;-&#34;)).join(&#34;,&#34;);
}

function generateRange(from, to) {
  if (from &lt; to) return [from, to];
  else return [from];
}
</code></pre> 
<p></p> 
<h4>Python算法源码</h4> 
<pre><code class="language-python"># 输入获取
vlanArr &#61; list(map(lambda x: list(map(int, x.split(&#34;-&#34;))), input().split(&#34;,&#34;)))
remove &#61; int(input())


def generateRange(start, end):
    if start &lt; end:
        return [start, end]
    else:
        return [start]


# 算法入口
def getResult():
    vlanArr.sort(key&#61;lambda x: x[0])

    for i in range(len(vlanArr)):
        vlan &#61; vlanArr[i]

        start &#61; vlan[0]
        if len(vlan) &gt; 1:
            end &#61; vlan[1]

            if remove &lt; start or remove &gt; end:
                continue

            vlanArr.pop(i)

            if remove &#61;&#61; start:
                vlanArr.insert(i, generateRange(remove &#43; 1, end))
            elif remove &#61;&#61; end:
                vlanArr.insert(i, generateRange(start, remove - 1))
            else:
                vlanArr.insert(i, generateRange(remove &#43; 1, end))
                vlanArr.insert(i, generateRange(start, remove - 1))

            break
        elif start &#61;&#61; remove:
            vlanArr.pop(i)
            break

    return &#34;,&#34;.join(list(map(lambda x: &#34;-&#34;.join(map(str, x)), vlanArr)))


# 算法调用
print(getResult())
</code></pre>
                </div>
        </div>
        <div id="treeSkill"></div>
        <div id="blogExtensionBox" style="width:400px;margin:auto;margin-top:12px" class="blog-extension-box"></div>
    <script>
  $(function() {
    setTimeout(function () {
      var mathcodeList = document.querySelectorAll('.htmledit_views img.mathcode');
      if (mathcodeList.length > 0) {
        for (let i = 0; i < mathcodeList.length; i++) {
          if (mathcodeList[i].naturalWidth === 0 || mathcodeList[i].naturalHeight === 0) {
            var alt = mathcodeList[i].alt;
            alt = '\\(' + alt + '\\)';
            var curSpan = $('<span class="img-codecogs"></span>');
            curSpan.text(alt);
            $(mathcodeList[i]).before(curSpan);
            $(mathcodeList[i]).remove();
          }
        }
        MathJax.Hub.Queue(["Typeset",MathJax.Hub]);
      }
    }, 1000)
  });
</script>
</div></html>